package org.jeecg.zhenxuan.Service.impl;

import org.jeecg.zhenxuan.Entity.UsersPoint;
import org.jeecg.zhenxuan.Repository.UserAddRepository;
import org.jeecg.zhenxuan.Service.UserAddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserAddServiceImpl implements UserAddService {

    @Autowired
    private UserAddRepository userRepository;

//    @Autowired
//    private UserRepository userRepository;

    /**
     * 更新用户豆子信息
     * @param openId
     * @param count
     * @return
     */
    @Override
    public UsersPoint updateUser(String openId,Integer count,String operation ) {

        //查询用户原有的豆子数量
        UsersPoint user = this.getUserByOpenId(openId);

        //计算修改后后的豆子数量
        if (user != null) {
            //该用户存在，则修改豆子数量
            if (operation.equals("add")) {
                //添加豆子，修改豆子数量
                int isSuccess = userRepository.updatePoint(count, openId);
//                int isSuccess = userRepository.updatePointStd(count, openId , channel ,new Date());
                if(isSuccess > 0){
                    user.setPointTotal(user.getPointTotal()+count);
                }
            } else if (operation.equals("reduce")) {
                //减少,暂时不用此逻辑
            }

        }else{
            //该用户不存在，创建该用户并添加豆子
            user = new UsersPoint();
            user.setOpenId(openId);
            user.setPointTotal(count);



            userRepository.save(user);
        }

        return user;
    }

    /**
     * 查询用户信息（豆子数量）
     * @param openId
     * @return
     */
    @Override
    public UsersPoint getUserByOpenId(String openId) {
        UsersPoint user = userRepository.getUserByOpenId(openId);
        return user;
    }

    /**
     * add by 2019年12月13日 星期五
     * 更新用户豆子信息
     * @param openId
     * @param count
     * @return
     */
    @Override
    public UsersPoint updateUserStd(String openId,Integer count,String operation , String appId , String channel , String phoneNumber ,String name) {

        //查询用户原有的豆子数量
        UsersPoint user = userRepository.findUsersPointByOpenIdAndChannel(openId ,channel);

        //计算修改后后的豆子数量
        if (user != null) {
            //该用户存在，则修改豆子数量
            if (operation.equals("add")) {
                //添加豆子，修改豆子数量
//                int isSuccess = userRepository.updatePoint(count, openId);
                int isSuccess = userRepository.updatePointStd(count, openId ,channel , new Date());
                if(isSuccess > 0){
                    user.setPointTotal(user.getPointTotal()+count);
                }
            } else if (operation.equals("reduce")) {
                //减少,暂时不用此逻辑
            }

        }else{
            //该用户不存在，创建该用户并添加豆子
            user = new UsersPoint();
            user.setOpenId(openId);
            user.setPointTotal(count);
            user.setAppId(appId);
            user.setChannel(channel);
            user.setPhoneNumber(phoneNumber);
            user.setCreateTime(new Date());
            user.setLastModifyTime(new Date());
            user.setName(name);

            userRepository.save(user);
        }

        return user;
    }







}
